const express = require('express')
const Router = express.Router()
const sqlHelper = require('../common/sqlHelper')


//查询所有
Router.get('/', (req, res) => {
    const sql = `select id,name,gender,img from heros where isDelete = 0`
    const callback = (err, result) => {
        if (err) {
            console.log('查询失败');
            res.send({ code: 500, msg: "查询失败", data: null })
        } else {
            console.log("查询成功");
            res.send({ code: 200, msg: "查询成功", data: result })
        }
    }
    sqlHelper(sql, callback)
})
//查询单个
Router.get('/:id', (req, res) => {
    const id = req.params.id
    // console.log(id);
    const sql = `select id,name,gender,img from heros where id = ${id} and  isDelete = 0 `

    const callback = (err, result) => {
        if (err) {
            console.log('查询失败');
            console.log(err);
            res.status(500).send({ code: 500, msg: "查询失败", data: null })
        } else {
            if (result.length) {
                res.send({ code: 200, msg: "查询成功", data: result[0] })
            } else {
                res.status(404).send({ code: 404, msg: "该数据id不存在", data: null })
            }
        }
    }
    sqlHelper(sql, callback)
})
//新增
Router.post('/', (req, res) => {
    // console.log(req.body);
    const { name, gender, img } = req.body
    if (!name || !gender || !img) {
        res.status(500).send({ code: 500, msg: "参数传递错误,请检查后重试", data: null })
        return
    }
    const sql = `insert into heros(name,gender,img,isDelete) values('${name}','${gender}','${img}',0)`
    const callback = (err, result) => {
        if (err) {
            console.log('新增失败');
            res.status(500).send({ code: 500, msg: "新增失败", data: null })
        } else {
            console.log("查询成功");
            res.status(200).send({ code: 200, msg: "新增成功", data: null })
        }
    }
    sqlHelper(sql, callback)
})

//修改
Router.put('/:id', (req, res) => {
    const id = req.params.id
    const { name, gender, img } = req.body
    //先查询来匹配是否有此id
    const querySql = `select id,name,gender,img from heros where id = ${id} and  isDelete = 0 `
    const queryCallback = (err1, result1) => {
        if (err1) {
            console.log('修改失败');
            console.log(err1);
            res.status(500).send({ code: 500, msg: "查询失败,该数据id不存在", data: null })
        } else {
            if (result1.length) {
                /*=========数据存在修改==========*/


                const sql = `update heros set name="${name}",gender="${gender}",img="${img}" where id=${id}`
                const callback = (err, result) => {
                    if (err) {
                        console.log('修改失败');
                        console.log(err);
                        res.status(500).send({ code: 500, msg: "修改失败", data: null })
                    } else {
                        console.log("修改成功");
                        res.status(200).send({ code: 200, msg: "修改成功", data: null })
                    }
                }
                sqlHelper(sql, callback)




                /*=========数据存在修改==========*/
            } else {
                res.status(404).send({ code: 404, msg: "该数据id不存在", data: null })
            }
        }
    }

    sqlHelper(querySql, queryCallback)








})

//删除
Router.delete('/:id', (req, res) => {
    const id = req.params.id

    //先查询来匹配是否有此id
    const querySql = `select id,name,gender,img from heros where id = ${id} and  isDelete = 0 `
    const queryCallback = (err1, result1) => {
        if (err1) {
            console.log('删除失败');
            console.log(err1);
            res.status(500).send({ code: 500, msg: "查询失败,该数据id不存在", data: null })
        } else {
            if (result1.length) {
                /*=========数据存在删除==========*/


                const sql = `update heros set isDelete = 1 where id=${id}`
                const callback = (err, result) => {
                    if (err) {
                        console.log('删除失败');
                        console.log(err);
                        res.status(500).send({ code: 500, msg: "删除失败", data: null })
                    } else {
                        console.log("删除成功");
                        res.status(200).send({ code: 200, msg: "删除成功", data: null })
                    }
                }
                sqlHelper(sql, callback)


                /*=========数据存在删除==========*/
            } else {
                res.status(404).send({ code: 404, msg: "该数据id不存在", data: null })
            }
        }
    }

    sqlHelper(querySql, queryCallback)
})

//上传
Router.post('/uploads', (req, res) => {
    res.send('上传');
})



module.exports = Router